package com.easy.auth.controller;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.easy.auth.domain.Userinfo;
import com.easy.auth.service.UserinfoService;
import com.easy.common.jdbc.PageInfo;
import com.easy.common.web.FilterUtil;
import com.easy.common.web.PagedList;
import com.easy.common.web.message.DeleteMessage;
import com.easy.common.web.message.Message;
import com.easy.common.web.message.SaveOrEditMessage;

@Controller
@RequestMapping("UserinfoController")
public class UserinfoController {

	@Autowired
	private UserinfoService userinfoService;

	@RequestMapping("goToUserManager")
	public String goToUserManager(String secondMenuId,HttpServletRequest request) {
		request.getSession().setAttribute("secondMenuId", secondMenuId);
		return "auth/user";
	}

	/**
	 * 查询用户集合
	 * 
	 * @param param
	 *            分页及过滤参数
	 * @return 列表信息集合
	 */
	@RequestMapping("getAllUsers")
	@ResponseBody
	public PagedList<Userinfo> getAllUsers(PageInfo param) {
		Userinfo userinfo = (Userinfo) FilterUtil.getVO(Userinfo.class,
				param.getFilterRules());
		//System.out.println(param.getFilterRules().toString());
		return userinfoService.findAllUserinfo(userinfo, param);
	}

	/**
	 * 删除指定用户
	 * 
	 * @param oids
	 *            删除用户的id集合
	 * @return
	 */
	@RequestMapping("removeUser")
	@ResponseBody
	public Message removeUser(String oids) {
		return new DeleteMessage(userinfoService.removeUser(oids));
	}

	/**
	 * 新增或编辑用户
	 * 
	 * @param userinfo
	 * @return
	 */
	@RequestMapping("saveOrEditUser")
	@ResponseBody
	public Message saveOrEditUser(Userinfo userinfo) {
		return new SaveOrEditMessage(
				userinfoService.saveOrEditUserinfo(userinfo));
	}

	/**
	 * 编辑用户时查询选择的用户信息
	 * 
	 * @param userinfo
	 * @return
	 */
	@RequestMapping("findUserifo")
	@ResponseBody
	public Userinfo findUserinfo(Userinfo userinfo) {
		return userinfoService.findUserinfo(userinfo);
	}

	/**
	 * 新增用户时检查用户名是否已经存在
	 * 
	 * @param userinfo
	 * @return 存在:true
	 */
	@RequestMapping("findUserById")
	@ResponseBody
	public boolean findUserById(Userinfo userinfo) {
		userinfo = userinfoService.findUserinfo(userinfo);
		/*if (userinfo != null) {
			return false;
		} else {
			return true;
		}*/
		return userinfo == null;
	}

	/**
	 * 获取部门已有用户列表
	 * 
	 * @param userid
	 * @param pageInfo
	 * @return
	 */
	@RequestMapping("getDepartmentHaveUserList")
	@ResponseBody
	public PagedList<Userinfo> getDepartmentHaveUserList(String depId,
			PageInfo pageInfo) {

		return userinfoService.getDepHaveUsersByUserid(depId, pageInfo);
	}

	/**
	 * 获取部门未分配用户列表
	 * 
	 * @param userid
	 * @param pageInfo
	 * @return
	 */
	@RequestMapping("getDepartmentNotHaveUserList")
	@ResponseBody
	public PagedList<Userinfo> getDepartmentNotHaveUserList(String depId,
			PageInfo pageInfo) {

		return userinfoService.getDepNotHaveUsersByUserId(depId, pageInfo);
	}

	/**
	 * 移除部门已有用户
	 * 
	 * @param userid
	 * @param roleids
	 * @return
	 */
	@RequestMapping("removeDepartmentHaveUser")
	@ResponseBody
	public DeleteMessage removeDepartmentHaveUser(String depId, String userids) {

		return new DeleteMessage(userinfoService.removeDepartmentUsers(depId,
				userids.split("-")));
	}

	/**
	 * 增加部门未分配用户
	 * 
	 * @param userid
	 * @param roleids
	 * @return
	 */
	@RequestMapping("addDeparmentNotHaveUser")
	@ResponseBody
	public SaveOrEditMessage addDeparmentNotHaveUser(String depId,
			String userids) {

		return new SaveOrEditMessage(userinfoService.addDepartmentUsers(depId,
				userids.split("-")));
	}
}
